package com.wuyong.Chapter3;

import com.wuyong.MyArrayList;

public class ArrayStack<E> implements Stack<E> {


    private MyArrayList arrayList;
    public ArrayStack(int capacity) {
        arrayList = new MyArrayList(capacity);
    }
    public ArrayStack() {
        this(10);
    }
    @Override
    public int getSize() {
        return arrayList.getSize();
    }

    @Override
    public boolean isEmpty() {
        return arrayList.isEmpty();
    }

    @Override
    public void push(E e) {
        arrayList.addLast(e);
    }

    @Override
    public E pop() {
        return (E) arrayList.removeLast();
    }

    @Override
    public E peek() {
        return (E) arrayList.getLast();
    }

    public int getCapacity() {
        return arrayList.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Stack: ");
        sb.append('[');
        for (int i = 0; i < arrayList.getSize(); i++) {
            sb.append(arrayList.get(i));
            if (i != arrayList.getSize() -1) {
                sb.append(", ");
            }
        }
        sb.append("] top");
        return sb.toString();
    }

}
